package com.fivestar.pmanager.core.service;

import com.fivestar.pmanager.core.base.BaseBean;
import com.fivestar.pmanager.core.base.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.util.List;
import static com.fivestar.pmanager.core.util.LoggerHelper.logError;
import static com.fivestar.pmanager.core.util.CodeHelper.isNotNullOrEmpty;


@SuppressWarnings("all")
public abstract class BaseServiceImpl<T extends BaseBean> implements BaseService<T>
{
    private static final Logger logger = LoggerFactory.getLogger(BaseServiceImpl.class);


    /**
     * 分页模板方法定义
     *
     * @author shangk
     * @return
     * @throws
     */
    @Override
    public void page(T parameter_, PageBean page)
    {
        int count = getCountOfPage(parameter_);
        page.setTotal(count);
        //计算总页数，并计算分页数据起始索引
        int startIndex = page.calcPages().calcStartIndex();
        parameter_.setStartIndex(startIndex);
        parameter_.setOffset(page.getPageSize());

        List<T> rows = getListOfPage(parameter_);
        page.setRows(rows);
    }


    @Override
    public void page(BaseBean parameter_, PageBean page, String countMethodName,
            String listMethodName)
    {
        try
        {
            Method countMethod = null;
            Method listMethod = null;
            Method[] methods = this.getClass().getMethods();
            for (Method method : methods)
            {
                if (method.getName().equals(countMethodName))countMethod = method;
                if (method.getName().equals(listMethodName))listMethod = method;
            }
            if (countMethod == null) throw new Exception("数量统计方法不能为空！检查类中方法是否存在“" + countMethodName + "”");
            if (listMethod == null) throw new Exception("列表统计方法不能为空！检查类中方法是否存在“" + listMethodName + "”");

            int count = (int) countMethod.invoke(this,parameter_);
            page.setTotal(count);
            //计算总页数，并计算分页数据起始索引
            int startIndex = page.calcPages().calcStartIndex();
            parameter_.setStartIndex(startIndex);
            parameter_.setOffset(page.getPageSize());

            List<? extends BaseBean> rows = (List<? extends BaseBean>) listMethod.invoke(this,parameter_);
            page.setRows(rows);
        }
        catch (Exception e)
        {
            logError(logger, e);
        }

    }


    @Override
    public void advancedPage(T parameter_, PageBean pageBean)
    {
        //计算排序
        String orderBy = "";
        if(parameter_ != null && isNotNullOrEmpty(parameter_.getSortField()))
        {
            orderBy = parameter_.getSortField() + (isNotNullOrEmpty(parameter_.getSortBy()) ? parameter_.getSortBy() : "asc");
        }
        parameter_.setSortBy(null);
        parameter_.setSortField(null);
        Page<T> page = PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize(), orderBy);
        getList(parameter_);
        pageBean.setPages(page.getPages());
        pageBean.setTotal(Long.valueOf(page.getTotal()).intValue());
        pageBean.setRows(page.getResult());
        pageBean.calcPages();
    }


    @Override
    public void advancedPage(T parameter_, PageBean pageBean, String listMethodName)
    {
        //计算排序
        String orderBy = "";
        if(parameter_ != null && isNotNullOrEmpty(parameter_.getSortField()))
        {
            orderBy = parameter_.getSortField() + (isNotNullOrEmpty(parameter_.getSortBy()) ? parameter_.getSortBy() : "asc");
        }
        parameter_.setSortBy(null);
        parameter_.setSortField(null);
        Page<T> page = PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize(), orderBy);
        try
        {
            Method listMethod = null;
            Method[] methods = this.getClass().getMethods();
            for (Method method : methods)
            {
                if (method.getName().equals(listMethodName))listMethod = method;
            }
            if(listMethod != null) listMethod.invoke(this, parameter_);

        }
        catch (Exception e)
        {
            logError(logger, e);
        }
        pageBean.setPages(page.getPages());
        pageBean.setTotal(Long.valueOf(page.getTotal()).intValue());
        pageBean.setRows(page.getResult());
        pageBean.calcPages();
    }


}
